package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
 * @author: 爱学Java 不想秃头的 KK
 * @date: 2023/10/13 11:21
 * @version: 1.0
 * @Created with IntelliJ IDEA.
 * @Description:
 */


@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1.配置跨越
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        //2.是否允许携带cooke进行跨域
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }
    // @Bean
    // public CorsWebFilter corsWebFilter(){
    //     UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //     CorsConfiguration corsConfiguration = new CorsConfiguration();
    //     //1.配置跨越
    //     corsConfiguration.addAllowedHeader("*");
    //     corsConfiguration.addAllowedMethod("*");
    //     corsConfiguration.addAllowedOrigin("*");
    //     corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8001"));
    //     corsConfiguration.setAllowedOriginPatterns(Arrays.asList("http://*"));
    //     //2.是否允许携带cooke进行跨域
    //     corsConfiguration.setAllowCredentials(true);
    //     source.registerCorsConfiguration("/**",corsConfiguration);
    //     return new CorsWebFilter(source);
    // }
}
